import React, { useState, useEffect, useRef } from 'react';
import { connect } from 'umi'
import { Tree } from 'antd'
import { Modal, Form, notification } from 'antd';
const ModelForm = (props) => {
  const { visible = false, onClose, loading, dispatch, reload } = props;
  const { show = false, detail = [] } = visible;
  const [list, setList] = useState([])
  const setSort = () => {
    dispatch({
      type: 'liveStreaming/fakeGoodsContent',
      payload: {
        sortGoodsList: list.reduce((item, val) => {
          const { list, count } = item
          const { children = [], key } = val
          let num = count;
          num += 1
          let obj = {
            list: [
              ...list, {
                id: key,
                sortValue: num,
              },
              ...children.map(item => {
                num += 1
                return {
                  id: item.key,
                  sortValue: num,
                  goodsSectionId: val.goodsSectionId
                }
              })
            ]
          }
          obj.count = num
          return obj
        }, { list: [], count: 0 }).list
      },
      callback: () => {
        onClose();
        reload()
      }
    })
  }
  useEffect(() => {
    if (detail.length) {
      setList(detail)
    }
  }, [detail])
  console.log(list, detail)
  const modalProps = {
    title: `自定义排序`,
    width: 950,
    visible: show,
    maskClosable: true,
    destroyOnClose: true,
    zIndex: 1001,
    confirmLoading: loading,
  };
  const onDrop = (info) => {
    const dropKey = info.node.key;
    const dragKey = info.dragNode.key;
    const dropPos = info.node.pos.split('-');
    const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
    const loop = (data, key, callback) => {
      for (let i = 0; i < data.length; i++) {
        if (data[i].key === key) {
          return callback(data[i], i, data);
        }
        if (data[i].children) {
          loop(data[i].children, key, callback);
        }
      }
    };
    const data = [...list];

    // Find dragObject
    let dragObj;
    loop(data, dragKey, (item, index, arr) => {
      arr.splice(index, 1);
      dragObj = item;
    });
    if (!info.dropToGap) {
      // Drop on the content
      loop(data, dropKey, (item) => {
        item.children = item.children || [];
        // where to insert 示例添加到头部，可以是随意位置
        item.children.unshift(dragObj);
      });
    } else if (
      (info.node.props.children || []).length > 0 &&
      // Has children
      info.node.props.expanded &&
      // Is expanded
      dropPosition === 1 // On the bottom gap
    ) {
      loop(data, dropKey, (item) => {
        item.children = item.children || [];
        // where to insert 示例添加到头部，可以是随意位置
        item.children.unshift(dragObj);
        // in previous version, we use item.children.push(dragObj) to insert the
        // item to the tail of the children
      });
    } else {
      let ar = [];
      let i;
      loop(data, dropKey, (_item, index, arr) => {
        ar = arr;
        i = index;
      });
      if (dropPosition === -1) {
        ar.splice(i, 0, dragObj);
      } else {
        ar.splice(i + 1, 0, dragObj);
      }
    }
    setList(data);
  };
  return (
    <Modal {...modalProps} onCancel={onClose} onOk={() => { setSort() }}>
      <Tree
        className="draggable-tree"
        draggable
        blockNode
        onDrop={onDrop}
        treeData={list}
      />
    </Modal>
  );
};

export default connect(({ loading }) => ({
  loading: loading.effects['liveStreaming/fakeGoodsContent']
}))(ModelForm);;

